<?php
/**
 * Created by PhpStorm.
 * User: DaveTang
 * Date: 2018/2/1
 * Time: 15:52
 * 直播控制组件
 */

namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Http\Client;
use Cake\Core\Configure;

class LiveComponent extends Component
{
    protected $_defaultConfig = [
        'appId' => '1303084346',
        'bizId' => '114584',//腾讯分配的直播id
        'key' => '2ed8a97ddfba316cfa3988a61f647242',//加密直播链接的key
        'apiKey' => '2ed8a97ddfba316cfa3988a61f647242'//请求直播状态的key
    ];

    public function initialize(array $config)
    {

    }

    /* 获取推流地址
     * $streamId : 直播的id
     * $time:直播有效时间
     * @return 推流地址
    */
    public function getPushUrl($streamId, $time = null)
    {
        $key = $this->getConfig('key');
        $bizId = $this->getConfig('bizId');
        $liveCode = $bizId. '_' .$streamId; //直播码
        if($time){
            $txTime = strtoupper(base_convert($time,10,16));
            $txSecret = md5($key.$liveCode.$txTime);
            $ext_str = '?'.http_build_query(array(
                    'bizid' => $bizId,
                    'txSecret' => $txSecret,
                    'txTime' => $txTime
                ));
        }
        return 'rtmp://livepush.wtimall.com/live/' .$liveCode . (isset($ext_str) ? $ext_str : '');
    }

    /* 获取播放地址
     * $streamId : 直播的id
     * @return 3种播放器的地址
    */
    public function getPlayUrl($streamId)
    {
        $website = Configure::read('Website');
        $mainPath = $website['liveUrl'];
        $bizId = $this->getConfig('bizId');
        $liveCode = $bizId . '_' . $streamId; //直播码
        return array(
            'rtmp://'.$mainPath.'/live/'.$liveCode,
            'https://'.$mainPath.'/live/'.$liveCode.'.flv',
            'https://'.$mainPath.'/live/'.$liveCode.'.m3u8');
    }

    /* 禁用/关闭/开启推流
     * $streamId : 直播的id
     * $status: 设置的状态，0 表示禁用，1 表示允许推流，2 表示断流
     * $t:直播有效时间，类型为时间格式“YYYY-mm-dd HH:ii:ss”
     * @return Array,其中status代表状态，200为成功，400为失败；msg代表返回的信息
    */
    public function setStreamStatus($streamId, $status, $t) {
        $appid = $this->getConfig('appId');
        $key = $this->getConfig('apiKey');
        $bizId = $this->getConfig('bizId');
        $liveCode = $bizId . '_' . $streamId; //直播码
//        $t = strtotime($t);
        $http = new Client();
        $response = $http->get('http://fcgi.video.qcloud.com/common_access', [
            'appid' => $appid,
            'interface' => 'Live_Channel_SetStatus',
            'Param.s.channel_id' => $liveCode,
            'Param.n.status' => $status,
            't' => $t,
            'sign' => md5($key . $t)
        ]);

        $re = json_decode($response->body(), true);
        if(is_array($re)) {
            $s = isset($re['ret'])? $re['ret'] : 400;
            $s = $s === 0 ? 200 : 400;
            $m = isset($re['message'])? $re['message'] : 'unknown error';
        } else {
            $s = 400;
            $m = $response->body();
        }

        return $this->_returnMsg($s, $m);
    }

    private function _returnMsg ($status, $msg = null) {
        return [
            'status' => $status,
            'msg' => $msg
        ];
    }
}